<?php

class FormControl {
		
	function create($type,$list,$data=array()){
		$str = '';
		if($type=='edit'){
			foreach($list as $k=>$v){
				$v['name'] = $k;
				if(!$v['value']) $v['value'] = $data[$k];
				//if(!$v['value']) $v['value'] = $_GET[$v['field']];
				$str .= $this->edit($v);
			}
		}
		if($type=='search'){
			foreach($list as $k=>$v){
				$v['name'] = $v['field'];
				if(!$v['value']) $v['value'] = $_GET[$v['field']];
				$str .= $this->search($v);
			}
		}
		return $str;
	}
	
	function search($data){
		//$str = '<tr>';
		$str .= $this->search_lable($data['title']);
		$str .= $this->search_value($data);
		//$str .= '</tr>';
		return $str;
	}
	function search_lable($name){
		//$str = '<td class="td_label">';
		$str = $name.": ";
		//$str = '</td>';
		return $str;
	}
	
	function search_value($data){
		//$str = '<td class="td_value" onmouseover="this.style.backgroundColor=\'#FBF0E3\';" onmouseout="this.style.backgroundColor=\'#FFFFFF\'">';
		$str .= $this->$data['type']($data);
		//$str .= '</td>';
		return $str;
	}
	
	function edit($data){
		$str = '<tr>';
		$str .= $this->edit_lable($data['title']);
		$str .= $this->edit_value($data);
		$str .= '</tr>';
		return $str;
	}
	function edit_lable($name){
		$str = '<td class="td_label">'.$name.'</td>';
		return $str;
	}
	
	function edit_value($data){
		$str = '<td class="td_value" onmouseover="this.style.backgroundColor=\'#FBF0E3\';" onmouseout="this.style.backgroundColor=\'#FFFFFF\'">';
		$str .= $this->$data['type']($data);
		$str .= '</td>';
		return $str;
	}
	
	function button($data){
		//_e($data);
		$click = '';
		if($data['onclick']) $click = ' onclick="'.$data['onclick'].'"';
		$str = '<input id="'.$data['name'].'" name="'.$data['name'].'"'.$click.' type="button" value="'.$data['value'].'">';
		return $str;
	}
	
	function text($data){
		$focus = empty($data['onfocus']) ? '' : ' onfocus="'.$data['onfocus'].'"';
		$width = empty($data['width']) ? '300' : $data['width'];
		$readonly = empty($data['readonly']) ? '' : ' readonly="readonly"';
		$autoverify = '';
		if($data['autoverify']) $autoverify = ' autoverify="'.$data['autoverify'].'"';
		$str = '<input type="text"'.$focus.$readonly.$autoverify.' id="'.$data['name'].'" name="'.$data['name'].'" value="'.$data['value'].'" class="ac_input" style="width:'.$width.'px" />';
		return $str;
	}
	
	function textarea($data){
		$width = empty($data['width']) ? '300' : $data['width'];
		$height = empty($data['height']) ? '50' : $data['height'];
		$onblur = '';
		if($data['onblur']) $onblur = ' onblur="'.$data['onblur'].'"';
		$autoverify = '';
		if($data['autoverify']) $autoverify = ' autoverify="'.$data['autoverify'].'"';
		$str = '<textarea name="'.$data['name'].'"  id="'.$data['name'].'"'.$onblur.$autoverify.' style="width:'.$width.'px;height:'.$height.'px">'.$data['value'].'</textarea>';
		return $str;
	}
	
	function radio($data){
		if(!$data['value']&&$data['default']) $data['value'] = $data['default'];
		$str = '';
		preg_match('/<txt>(.*)<\/txt>/Us',$data['source'],$source);
		if($source[1]){
			preg_match_all('/<option>(.*)<\/option>/U',$source[1],$option);
			foreach($option[1] as $ko=>$o){
				$row = explode(":",$o);
				$sel = '';
				if($row[1] == $data['value']) $sel = ' checked="checked"';
				$str .= '<input name="'.$data['name'].'" type="radio" value="'.$row[1].'"'.$sel.' />'.$row[0];
			}
		}
		return $str;
	}	
	
	function checkbox($data){
		if(!$data['column']) $data['column'] = 2;
		$autoverify = '';
		if($data['autoverify']) $autoverify = ' autoverify="'.$data['autoverify'].'"';
		if(!$data['value']&&$data['default']) $data['value'] = $data['default'];
		$str = '';
		$width = '';
		if($data['width']) $width = 'width:'.$data['width'].'px;';
		preg_match('/<txt>(.*)<\/txt>/Us',$data['source'],$source);
		if($source[1]){
			preg_match_all('/<option>(.*)<\/option>/U',$source[1],$option);
			foreach($option[1] as $ko=>$o){
				$row = explode(":",$o);
				$sel = '';
				if(@in_array($row[1],$data['value'])||$row[1] == $data['value']) $sel = ' checked="checked"';
				if($data['column']==1){
					$str .= '<input type="checkbox"'.$autoverify.' name="'.$data['name'].'[]" value="'.$row[1].'"'.$sel.' />';
				}else{
					$str .= '<div style="float:left;'.$width.'"><input type="checkbox"'.$autoverify.' name="'.$data['name'].'[]" value="'.$row[1].'"'.$sel.' />'.$row[0]."</div>";
				}
				if(($i+$data['column']+1)%$data['column']==0) $str .= '<div style="clear:both"></div>';
				$i++;
			}
		}
		return $str;
	}
	
	function searchtext($data){
		$width = empty($data['width']) ? '300' : $data['width'];
		$str = '<input type="text" id="'.$data['name'].'" name="'.$data['name'].'" value="'.$data['value'].'" class="ac_input" style="width:'.$width.'px" onkeyup="searchtxt(this,\''.$data['act'].'\',\''.$data['hidename'].'\')" autocomplete="off" />';
		$row = array('name'=>$data['hidename'],'value'=>$_GET[$data['hidename']]);
		$str .= $this->hidden($row);
		return $str;
	}
	
	function select($data){
		$autoverify = '';
		if($data['autoverify']) $autoverify = ' autoverify="'.$data['autoverify'].'"';
		$change = '';
		if($data['change']) $change = ' onchange="'.$data['change'].'"';
		$str = '<select id="'.$data['name'].'" name="'.$data['name'].'"'.$change.$autoverify.'>';
		if($data['source']){
			if(is_array($data['source'])){
				foreach($data['source'] as $k=>$v){
					$sel = '';
					if($v == $data['value']) $sel = ' selected="selected"';
					$str .= '<option value="'.$k.'"'.$sel.'>'.$v.'</option>';
				}
			}else{
				preg_match('/<txt>(.*)<\/txt>/Us',$data['source'],$source);
				if($source[1]){
					preg_match_all('/<option>(.*)<\/option>/U',$source[1],$option);
					foreach($option[1] as $o){
						$row = explode(":",$o);
						$sel = '';
						if($row[1] == $data['value']) $sel = ' selected="selected"';
						$str .= '<option value="'.$row[1].'"'.$sel.'>'.$row[0].'</option>';
					}
				}else{
					preg_match('/<fun>(.*)<\/fun>/Us',$data['source'],$source);
					if($source[1]){
						$fun = $source[1];
						$list = call_user_func($fun);
						$str .= '<option value="">All</option>';
						if(count($list)>0){
							foreach($list as $k=>$v){
								if($k){
									$sel = '';
									if($k == $data['value']) $sel = ' selected="selected"';
									$str .= '<option value="'.$k.'"'.$sel.'>'.$v.'</option>';
								}
							}
						}
					}
				}
			}
		}
		$str .= '</select>';
		return $str;
	}
	
	function hidden($data){
		$str = '<input type="hidden" id="'.$data['name'].'" name="'.$data['name'].'" value="'.$data['value'].'" />';
		return $str;
	}
	
}

?>